import { http, HTTP_200, HTTP_201 } from '@/store/api'

const state = {
  authorItems: []
}

const getters = {
}

const mutations = {
  fetchAuthorItems (state, payload) {
    state.authorItems = payload
  },
  createAuthorItem (state, payload) {
    state.authorItems.push(payload)
  }
}

const actions = {
  fetchAuthorItems ({commit}) {
    http.get(`/authors/`).then(res => {
      if (res.status === HTTP_200) {
        commit('fetchAuthorItems', res.data)
      }
    })
  },
  createAuthorItem ({commit}, payload) {
    http.post(`/authors/`, payload).then(res => {
      if (res.status === HTTP_201) {
        commit('createAuthorItem', res.data)
      }
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
